# include <stdio.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <string.h>
# include "adddel.h"
# include "Stypes.h"
# include "databasectl.h"
//管理员删除用户
//调用该函数后，首先打印所有用户的用户名，发给客户端
//buf接受到客户端想要删除的值，付给mem_info结构提的name
//调用del_user函数
//成功返回1，并向客户端发送信息
void manager_del_user(struct member * mem)
{
	int len = 0;
	int socket = mem->fd;
	char * name_del ;
	char buf3[20] = "删除失败！";
	char buf2[20] = "删除成功！";
	char buf1[30] = "请选择你要删除的用户"; 
	char buf[30] = {0};//暂存客户端的选择
	struct mem_info mem_info;
	get_all_name(&name_del);//得到所有的用户名
	//通过socket与客户端进行交互
	strcat(strcat(name_del,buf1),"\n");
	len = strlen(name_del)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,name_del,len,0);
	
	free_all_name(name_del);
	//接收客户端选择删除的用户，通过buf
	recv(socket,&len,sizeof(int),0);
	recv(socket,buf,len,0);
	strncpy(mem_info.name,buf,len);
	if(del_user(&mem_info) == 1)
	{
		len = strlen(buf2)+1;
		send(socket,&len,sizeof(int),0);
		send(socket,buf2,len,0);
	}
	else
	{
		len = strlen(buf3)+1;
		send(socket,&len,sizeof(int),0);
		send(socket,buf3,len,0);
	}

}

void manager_add_user(struct member * mem)
{
	int len = 0;
	int socket = mem->fd;
	char buf[40] = "添加成功！";
	char buf1[40] = "添加失败 ";
	char bname[40] = "请输入你要添加的用户名：";
	char bpasswd[30] = "请输入用户密码：";
	char baddress[30] = "请输入用户地址：";
	char bphone[30] = "请输入用户电话：";
	char bpermission[100] = "请输入用户身份（0为普通用户，1为管理员）";
	char bage[40] = "请输入用户年龄：";
	char bsalary[40] = "请输入用户薪水：";

	struct mem_info mem_info;

	//请输入用户密码	
	len = strlen(bpasswd)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,bpasswd,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,mem_info.passwd,len,0);
	//请输入用户地址
	len = strlen(baddress)+1;
	send(socket,&len,len,0);
	send(socket,baddress,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,mem_info.address,len,0);
	//请输入用户电话
	len = strlen(bphone)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,bphone,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket, mem_info.phone,len,0);
	//权限
	len = strlen(bpermission)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,bpermission,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,&mem_info.permission,len,0);
	//年龄
	len = strlen(bage)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,bage,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,&mem_info.age,len,0);
	//薪水
	len = strlen(bsalary)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,bsalary,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,&mem_info.salary,len,0);

	if(add_user(&mem_info) == 1)
	{
		len = strlen(buf)+1;
		send(socket,&len,sizeof(len),0);
		send(socket,buf,len,0);
		recv(socket,&len,sizeof(int),0);
	}
	else
	{
		send(socket,buf1,strlen(buf1),0);
		recv(socket,&len,sizeof(int),0);
	}
}


//普通用户修改自己的信息
//1.服务器发送本用户的信息给客户端
//2.发送选择菜单1.name 2.pass 3.address 4.phone 5.permission 6.age 7.salary
//3.用buf接受客户端的选择，为数字
//4.输入修改的信息，发送给服务器
//5.用modify_table_byname进行修改
void change_user(struct member * member)
{
	int len = 0;
	int socket = member->fd;
	char buf[30] = {0};//用来接受客户端的选项
	char bvalue[30] = {0};
	char buf1[100] = "你可以修改的信息： 1.pass 2.address 3.phone 4.permission 5.age 6.salary";
	char buf2[100] = "请输入新的信息";
	char buf3[20] = "修改成功";
	char buf4[20] = "修改失败";

	struct mem_info mem_info;
	//发送用户当前信息给客户端
	select_table_byname(member->name,&mem_info);
	len = sizeof(mem_info);
	send(socket,&len,sizeof(int),0);
	send(socket,&mem_info,sizeof(mem_info),0);
	recv(socket,&len,sizeof(int),0); //返回一个数子或者长度，任意

	//发送选择菜单给客户端
	len = strlen(buf1)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,buf1,len,0);
	//接受客户端的选择
	recv(socket,&len,sizeof(int),0);
	recv(socket,buf,len,0);
	//新信息提
	len = strlen(buf2)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,buf2,len,0);
	//接受客户端修改后的信息
	recv(socket,&len,sizeof(int),0);
	recv(socket,bvalue,len,0);
	
	//数据库修改
	if(modify_table_byname(member->name,buf,bvalue) == 1)
	{
		len = sizeof(buf3);
		send(socket,&len,sizeof(int),0);
		send(socket,buf3,sizeof(buf3),0);	
		recv(socket,&len,sizeof(int),0);
	}else
	{
		len = sizeof(buf4);
		send(socket,&len,sizeof(int),0);
		send(socket,buf4,sizeof(buf4),0);
		recv(socket,&len,sizeof(int),0);
	}
	//修改后信息的显示
	select_table_byname(member->name,&mem_info);
	len = sizeof(mem_info);
	send(socket,&len,sizeof(int),0);
	send(socket,&mem_info,sizeof(mem_info),0);
	//jieshouxia
	recv(socket,&len,sizeof(int),0); //接受任意数
}

void change_manager(struct member * member)
{
	int len = 0;
	int socket = member->fd;
	char bname[30] = {0};//保存管理员修改的用户名
	char * buf = NULL;
	char tembuf[30] = {0};//用来接受客户端的选项
	char value[30] = {0}; //接受客户端修改后的信息
	char tishibuf[30] = "请输入新的信息";
	char buf1[30] = "你要修改的用户：";
	char buf2[100] = "你可以修改的信息：1.name 2.pass 3.address 4.phone 5.permission 6.age 7.salary";
	char buf3[20] = "修改成功";
	char buf4[20] = "修改失败";
	
	struct mem_info mem_info;

	get_all_name(&buf);
	//向客户端发送所有的用户名子
	len = strlen(buf)+1;//长度的实际长度不太确定
	send(socket,&len,sizeof(int),0);
	send(socket,buf,len,0);
	
	free_all_name(buf);
	recv(socket,&len,sizeof(len),0); //任意的数
//发送给客户端提示信息：选择修改的用户
	len = strlen(buf1)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,buf1,len,0);
	recv(socket,&len,sizeof(int),0);
	recv(socket,bname,len,0);

	//发送用户当前信息给客户端
	select_table_byname(bname,&mem_info);
	len = sizeof(mem_info);
	send(socket,&len,sizeof(int),0);
	send(socket,&mem_info,len,0);
	recv(socket,&len,sizeof(int),0);//接受一个任意数
	//发送选择菜单给客户端
	len = strlen(buf2)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,buf2,len,0);

	//接受客户端的选择
	recv(socket,&len,sizeof(int),0);
	recv(socket,tembuf,len,0);
	//新信息提示
	len = strlen(tishibuf)+1;
	send(socket,&len,sizeof(int),0);
	send(socket,tishibuf,len,0);
	
	//接受客户端修改后的信息
	recv(socket,&len,sizeof(int),0);
	recv(socket,value,len,0);


	//数据库修改
	if(modify_table_byname(bname,tembuf,value) == 1)
	{
		len = strlen(buf3)+1;
		send(socket,&len,sizeof(int),0);
		send(socket,buf3,len,0);	
		recv(socket,&len,sizeof(int),0);
	}else
	{
		len = strlen(buf4)+1;
		send(socket,&len,sizeof(int),0);
		send(socket,buf4,len,0);
		recv(socket,&len,sizeof(int),0);
	}
	//修改后信息的显示
	select_table_byname(bname,&mem_info);
	len = sizeof(mem_info);
	send(socket,&len,sizeof(int),0);
	send(socket,&mem_info,len,0);
	
	recv(socket,&len,sizeof(int),0); //接受任意数
}
